/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import corba.CorbaConfig;
import java.awt.event.ItemEvent;
import static java.awt.image.ImageObserver.WIDTH;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author Paul
 */
public class ConnectForm extends javax.swing.JFrame {

    /**
     * Creates new form ConnectForm
     */
    public ConnectForm() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnConnectOk = new javax.swing.JButton();
        btnConnectCancel = new javax.swing.JButton();
        ckbConnectIsLocalhost = new javax.swing.JCheckBox();
        txtConnectHostname = new javax.swing.JTextField();
        txtConnectPort = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel2.setText("Port");

        jLabel3.setText("Hostname");

        btnConnectOk.setText("Connect");
        btnConnectOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConnectOkActionPerformed(evt);
            }
        });

        btnConnectCancel.setText("Cancel");
        btnConnectCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConnectCancelActionPerformed(evt);
            }
        });

        ckbConnectIsLocalhost.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                ckbConnectIsLocalhostItemStateChanged(evt);
            }
        });
        ckbConnectIsLocalhost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ckbConnectIsLocalhostActionPerformed(evt);
            }
        });

        jLabel4.setText("Use localhost");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jLabel4)
                        .addGap(29, 29, 29)
                        .addComponent(ckbConnectIsLocalhost))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel3)
                        .addGap(45, 45, 45)
                        .addComponent(txtConnectHostname, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2)
                        .addGap(73, 73, 73)
                        .addComponent(txtConnectPort, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(127, 127, 127)
                        .addComponent(btnConnectOk)
                        .addGap(6, 6, 6)
                        .addComponent(btnConnectCancel)))
                .addContainerGap(51, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(jLabel4))
                    .addComponent(ckbConnectIsLocalhost))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel3))
                    .addComponent(txtConnectHostname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2))
                    .addComponent(txtConnectPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnConnectOk)
                    .addComponent(btnConnectCancel))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ckbConnectIsLocalhostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ckbConnectIsLocalhostActionPerformed
    }//GEN-LAST:event_ckbConnectIsLocalhostActionPerformed

    private void btnConnectCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnConnectCancelActionPerformed

    private void ckbConnectIsLocalhostItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_ckbConnectIsLocalhostItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            txtConnectHostname.setEnabled(false);
            txtConnectPort.setEnabled(false);
        } else {
            txtConnectHostname.setEnabled(true);
            txtConnectPort.setEnabled(true);
        }

    }//GEN-LAST:event_ckbConnectIsLocalhostItemStateChanged

    private void btnConnectOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectOkActionPerformed
        CorbaConfig config = new CorbaConfig();
        boolean isLocal = ckbConnectIsLocalhost.isSelected();
        String hostname = txtConnectHostname.getText();
        int port;
        try {
            port = Integer.parseInt(txtConnectPort.getText());
        } catch (NumberFormatException ne) {
            port = 6969;
        }
        if (isLocal) {
            config.setLocalhost(true);
        }
        if (!hostname.equals("")) {
            config.setHostname(hostname);
        }
        config.setPort(port);
        try {
            utils.Util.saveConfig(config);
            WelcomeForm wel;
            if (isLocal) {
                wel = new WelcomeForm();
            } else {
                wel = new WelcomeForm(config.getHostname(), config.getPort());
            }
            wel.setVisible(true);
            this.dispose();

        } catch (FileNotFoundException ex) {
            Logger.getLogger(ConnectForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ConnectForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnConnectOkActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {
            CorbaConfig config = utils.Util.loadfConfig();
            if (config == null) {
                return;
            }
            boolean isLocalhost = config.isLocalhost();
            ckbConnectIsLocalhost.setSelected(isLocalhost);
            if (!isLocalhost) {
                txtConnectHostname.setText(config.getHostname());
                txtConnectPort.setText(config.getPort() + "");
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ConnectForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ConnectForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ConnectForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formWindowOpened

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(WelcomeForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ConnectForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConnectCancel;
    private javax.swing.JButton btnConnectOk;
    private javax.swing.JCheckBox ckbConnectIsLocalhost;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtConnectHostname;
    private javax.swing.JTextField txtConnectPort;
    // End of variables declaration//GEN-END:variables
}
